// pkgTask/pages/task-takephoto-upload/index.js
const lbbTaskService = require('../service/task-service.js');
import fileUtil from '../../../utils/fileUtil.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    localId: null,
    fileList: [
      // {
      //   url: 'https://img.yzcdn.cn/vant/leaf.jpg',
      //   isImage: true,
      //   deletable: true,
      // },
      // {
      //   url: 'https://img.yzcdn.cn/vant/tree.jpg',
      //   isImage: true,
      //   deletable: false,
      // },
      // {
      //   url: 'https://img.yzcdn.cn/vant/tree.jpg',
      //   isImage: true,
      //   deletable: true,
      // },
    ],
    photoFlg: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("takephoto-upload--->", options)
      this.data.lbmId = options.lbmId
      this.data.hosRecordId = options.hosRecordId
      this.data.equRecordId = options.equRecordId
      this.setData({
        equId: options.equId,
        lockType: options.lockType,
        photoFlg: options.photoFlg === 'true',
        localId: `${this.data.lbmId}-${this.data.hosRecordId}-${this.data.equRecordId}`,
      })
      console.log('localId===>', this.data.localId)
      this.getEquPhotoFiles()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 文件超出大小限制	
   */
  oversize() {
    wx.showToast({
      title: '单个文件不能超过100MB',
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 文件读取完成后
   */
  afterRead(e) {
    let that = this
    const { file } = e.detail;
    const { fileList } = this.data;
    console.log("文件读取完--》", file)
    // wx.showLoading({
    //   title: '上传中...',
    // })
    file.lbmId = this.data.lbmId
    file.hosRecordId = this.data.hosRecordId
    file.equRecordId = this.data.equRecordId
    file.localId = this.data.localId
    file.id = -1
    fileList.push(file);
    that.setData({ fileList });

    // lbbTaskService.uploadPhotoFile(file ,function(res) {
    //      wx.hideLoading({
    //        success: (res) => {},
    //      })
    //      console.log("uploadPhotoFile-->", res, res["data"])                  
    //      if(res.result != 0) {
    //       wx.showModal({
    //         title: '提示',
    //         content: res.description,
    //       })
    //      }else {
    //       wx.showToast({
    //         title: '上传成功',
    //       })
    //       file.id = res.data                    
    //       fileList.push(file);
    //       that.setData({ fileList });
    //      }
         
    // })

  },

  /**
   * 删除图片	
   */
  delete(e) {
    const { fileList } = this.data;
    fileList.splice(e.detail.index, 1);
    this.setData({ fileList });
  },

  getEquPhotoFiles() {

    let that = this
    lbbTaskService.getEquPhotoUploadFiles({
      equRecordId: that.data.equRecordId
    }, function(res) {
      let serverFiles = res.data
      let localFiles = fileUtil.getFilesByLocalId(that.data.localId)
      that.setData({
        fileList: serverFiles.concat(localFiles)
      })
    })
  },

  commitUploadFile() {
     let that = this
     let idArr = new Array()
     let localFiles = new Array()
     console.log('文件列表===>', that.data.fileList)
     that.data.fileList.forEach(function(item){
       if (item.id != -1) {
        idArr.push(item.id)
       }else {
        localFiles.push(item)
       }
     })
     console.log("commitFile===>", idArr, localFiles)
     lbbTaskService.commitUploadFile({
      equRecordId: that.data.equRecordId,
      fileIdList: idArr
     }, function(res){
       console.log("commitFile-->", res)
       wx.showToast({
         title: res.result == 0 ? '提交成功' : res.description,
       })
       if(res.result == 0) {
         fileUtil.saveFilesByLocalId(that.data.localId, localFiles)
         wx.navigateBack({
           delta: 1,
         })
       }
     })
  },

})